package framework.spatial;

import framework.FRDController;
import framework.FRDModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.LinkedList;
import javax.swing.JButton;
import javax.swing.JOptionPane;

/**
 *
 * @author Lukáš Marek
 */
public class FRDSpatialController extends FRDController implements ActionListener
{
	protected FRDSpatial spatialPanel;
	protected FRDSpatialModel model;
	protected FRDSpatialMapBackground back;
	protected BufferedImage defaultPointImage = null;

	public FRDSpatialController(FRDSpatial panel, FRDModel model)
	{
		this.spatialPanel = panel;
		this.model = (FRDSpatialModel) model;
		initListeners();
	}
	
	/**
	 * inicializace listeneru
	 */
	private void initListeners()
	{
		spatialPanel.zoomOutButton.addActionListener(this);
		spatialPanel.zoomInButton.addActionListener(this);
		spatialPanel.addButton.addActionListener(this);
		spatialPanel.saveButton.addActionListener(this);
		spatialPanel.endPaintButton.addActionListener(this);
		spatialPanel.endPaintButton.setVisible(false);
		spatialPanel.deleteButton.addActionListener(this);
		spatialPanel.deleteButton.setVisible(false);
		spatialPanel.getSpatialMap().setController(this);
	}
	
	/**
	 * zpracovani kliknuti na nektere tlacitko
	 * @param e event
	 */
	@Override
	public void actionPerformed(ActionEvent e)
	{
		JButton button = (JButton) e.getSource();
		switch (button.getActionCommand())
		{
			case "addButton":
				String[] options = {"Čára", "Čtverec", "Bod", "Kruh", "Polygon"};
				int code = JOptionPane.showOptionDialog(spatialPanel, "Jaký prvek?", "Nový prvek", 0, JOptionPane.PLAIN_MESSAGE, null, options, null);
				FRDSpatialGItem item = null;
				switch (code)
				{
					case 0:
						item = new FRDSpatialGLine();
						break;
					case 1:
						item = new FRDSpatialGRectangle();
						break;
					case 2:
						item = new FRDSpatialGPoint();
						if (defaultPointImage != null) ((FRDSpatialGPoint)item).setImage(defaultPointImage);
						break;
					case 3:
						item = new FRDSpatialGCircle();
						break;
					case 4:
						item = new FRDSpatialGPolygon();
						break;
				}
				if (item != null)
					spatialPanel.getSpatialMap().addNewItem(item);
				break;
			case "zoomInButton":
				back.zoomIn();
				spatialPanel.repaint();
				break;
			case "zoomOutButton":
				back.zoomOut();
				spatialPanel.repaint();
				break;
			case "endPaintButton":
				spatialPanel.getSpatialMap().endPaint();
				break;
			case "deleteButton":
				model.deleteItem(spatialPanel.getSpatialMap().selectedItem);
				spatialPanel.getSpatialMap().selectedItem = null;
				selectedItem(null);
				loadItems();
				spatialPanel.repaint();
				break;
			default:
				System.err.println(button.getActionCommand());
				break;
		}
	}

	@Override
	protected LinkedList<String> setParameters()
	{ 
		return null;
	}
	
	/**
	 * Nacte z modelu data z databaze, data musi by ulozeny v seznamu (LinkedList) polozek typu FRDSpatialGItem
	 */
	@Override
	public void loadItems()
	{
		LinkedList<String> params = setParameters();
		LinkedList<FRDSpatialGItem> list = model.getData(params);
		for (FRDSpatialGItem item : list)
		{
			item.setBack(back);
		}
		spatialPanel.getSpatialMap().setData(list);
	}
	
	/**
	 * Nastavy vychozi obrazek pro pridavani bodu
	 * @param img 
	 */
	public void setDefaultPointImage(BufferedImage img)
	{
		this.defaultPointImage = img;
	}
	
	/**
	 * nastavi objekt starajici se o pozadi
	 * @param back 
	 */
	public void setBackgroundClass(FRDSpatialMapBackground back)
	{
		spatialPanel.getSpatialMap().setBackroundClass(back);
		this.back = back;
	}

	/**
	 * Funkce je zavolana pok je vybran nejaky objekt nebo je odznacen
	 * @param selectedItem 
	 */
	public void selectedItem(FRDSpatialGItem selectedItem)
	{
		if (selectedItem == null)
		{
			spatialPanel.deleteButton.setVisible(false);
		}
		else
		{
			spatialPanel.deleteButton.setVisible(true);
		}
	}
}
